<script>
	$(function(){
		Namespace.register("EBCMS.[namespace]cate");
		EBCMS.[namespace]cate.refresh = function refresh(){
			EBCMS.CORE.api({
				queryParams:{
					sort:'sort',
					order:'desc',
					__model:'navcate',
				},
				tree:true,
				treelevel:true,
				tpl:'[namespace]cate-table',
				target:'#[namespace]cate_main',
				compileAfter:function(data){
					$('#[namespace]cate_main').find('.input_sort').focus(function(){
						EBCMS.[namespace]cate.sortvalue = $(this).val();
					}).blur(function(){
						var $this = $(this);
						if ($this.val() != EBCMS.[namespace]cate.sortvalue) {
							EBCMS.ACT.togglefield('{:U('Navcate/resort')}',$this.data('id'),$this.val(),'[namespace]cate','resort');
						};
					});
				},
			});
		};
		EBCMS.[namespace]cate.save = function save(id){
			EBCMS.CORE.get({
				url:'{:U('Navcate/save')}',
				queryParams:{
					id:id,
				},
				target:'#lgModal .modal-content',
				loadAfter:function(data,p){
					$('#lgModal').modal('toggle');
				},
			});
		};
		EBCMS.[namespace]cate.add = function add(pid){
			EBCMS.CORE.get({
				url:'{:U('Navcate/add')}',
				queryParams:{
					pid:pid,
				},
				target:'#lgModal .modal-content',
				loadAfter:function(data,p){
					$('#lgModal').modal('toggle');
				},
			});
		};
		EBCMS.[namespace]cate.changenav = function changenav(category_id){
			EBCMS.[namespace].category_id = category_id;
			EBCMS.[namespace].queryParams.__where = {
				category_id:['eq',category_id],
			};
			EBCMS.[namespace].refresh();
		}
		EBCMS.[namespace]cate.refresh();
	});
</script>

<div class="header">
	<div class="header-title" onclick="EBCMS.[namespace]cate.refresh();">导航分类</div>
	<div class="btn-group pull-right" role="group" aria-label="...">
		<a class="btn btn-success" href="javascript:EBCMS.[namespace]cate.add()">添加</a>
	</div>
</div>
<div class="body" style="bottom:0px;" id="[namespace]cate_main">
</div>

<script id="[namespace]cate-table" type="text/html">
	<table class="table table-bordered">
		<tbody>
			<tr>
				<th>id</th>
				<th>名称</th>
				<th>排序</th>
				<th style="width:210px;">操作</th>
			</tr>
			{{include '[namespace]cate-table-item'}}
		</tbody>
	</table>
</script>
<script id="[namespace]cate-table-item" type="text/html">
	{{each rows as v n}}
		<tr>
			<td>{{v.id}}</td>
			<td>{{v.levelstr}}┣{{v.text}}</td>
			<td><input value="{{v.sort}}" class="input_sort" data-id="{{v.id}}"></td>
			<td>
			<div class="btn-group btn-group-xs" role="group" aria-label="...">
				{{if access(0,'[namespace]cate','lock')}}
					{{if v.locked==1}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.lock('{:U('Navcate/lock')}','{{v.id}}','0','[namespace]cate');">已锁</a>
					{{else}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.lock('{:U('Navcate/lock')}','{{v.id}}','1','[namespace]cate');">未锁</a>
					{{/if}}
				{{/if}}
				{{if access(v.locked,'[namespace]cate','status')}}
					{{if v.status==1}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.status('{:U('Navcate/status')}','{{v.id}}','0','[namespace]cate');">已审</a>
					{{else}}
					<a class="btn btn-info" href="javascript:EBCMS.ACT.status('{:U('Navcate/status')}','{{v.id}}','1','[namespace]cate');">未审</a>
					{{/if}}
				{{/if}}
				{{if access(v.locked,'[namespace]cate','save')}}
				<a class="btn btn-info" href="javascript:EBCMS.[namespace]cate.save('{{v.id}}');">编辑</a>
				{{/if}}
				{{if access(v.locked,'[namespace]cate','delete')}}
				<a class="btn btn-info" href="javascript:EBCMS.ACT.del('{:U('Navcate/delete')}','{{v.id}}','[namespace]cate');">删除</a>
				{{/if}}
				{{if access(0,'[namespace]','index')}}
				<a class="btn btn-info" href="javascript:EBCMS.[namespace]cate.changenav('{{v.id}}');" role="button">查看</a>
				{{/if}}
			</div>
			</td>
		</tr>
		{{if v.rows}}
			{{include '[namespace]cate-table-item' v}}
		{{/if}}
	{{/each}}
</script>